﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace XFControls.XFPanels.XFPanelItems
{
    public enum XFItemType
    {
        Display,
        Clickable,
        ClickableNonChange,
        Swipable
    }

    public enum XFItemClickResult
    {
        Nothing,
        SlideLeft,
        SlideRight,
        Confirm,
        CustomModal,
        TextInput
    }

    public enum XFItemTextAlign
    {
        Left,
        Right,
        Center,
        Justified
    }

    public enum XFItemBackgroundAlign
    {
        Center,
        Stretch,
        Top,
        Bottom
    }

    public enum XFItemState
    {
        Normal,
        Selected,
        SlideRight,
        SlideLeft
    }

    public class XFItemStyle : IDisposable
    {
        public XFItemTextAlign TextAlign { get; set; }
        public XFItemTextAlign SecondaryTextAlign { get; set; }

        public Color BackgroundColor { get; set; }
        public Color TextColor { get; set; }
        public Color SecondaryTextColor { get; set; }
        public Font TextFont { get; set; }
        public Font SecondaryTextFont { get; set; }
        public Bitmap BackgroundImage { get; set; }
        public XFItemBackgroundAlign BackgroundAlign { get; set; }

        public Color SelectedBackgroundColor { get; set; }
        public Color SelectedTextColor { get; set; }
        public Color SelectedSecondaryTextColor { get; set; }
        public Font SelectedTextFont { get; set; }
        public Font SelectedSecondaryTextFont { get; set; }
        public Bitmap SelectedBackgroundImage { get; set; }
        public XFItemBackgroundAlign SelectedBackgroundAlign { get; set; }

        public int Padding { set { PaddingBottom = value; PaddingLeft = value; PaddingRight = value; PaddingTop = value; } }
        public int PaddingTop { get; set; }
        public int PaddingBottom { get; set; }
        public int PaddingLeft { get; set; }
        public int PaddingRight { get; set; }

        public DashStyle DashStyleTop { get { return _dashStyleTop; } set { _dashStyleTop = value; BoarderTop = true; } }
        public DashStyle DashStyleBottom { get { return _dashStyleBottom; } set { _dashStyleBottom = value; BoarderBottom = true; } }
        public DashStyle DashStyleLeft { get { return _dashStyleLeft; } set { _dashStyleLeft = value; BoarderLeft = true; } }
        public DashStyle DashStyleRight { get { return _dashStyleRight; } set { _dashStyleRight = value; BoarderRight = true; } }

        private DashStyle _dashStyleTop;
        private DashStyle _dashStyleBottom;
        private DashStyle _dashStyleLeft;
        private DashStyle _dashStyleRight;

        public bool BoarderTop { get; private set; }
        public bool BoarderBottom { get; private set; }
        public bool BoarderLeft { get; private set; }
        public bool BoarderRight { get; private set; }

        public Color BoarderTopColor { get; set; }
        public Color BoarderBottomColor { get; set; }
        public Color BoarderLeftColor { get; set; }
        public Color BoarderRightColor { get; set; }

        public XFItemStyle()
        {
            BoarderBottomColor = Color.Black;
            BoarderLeftColor = Color.Black;
            BoarderRightColor = Color.Black;
            BoarderTopColor = Color.Black;
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (TextFont != null)
                TextFont.Dispose();
            if (SecondaryTextFont != null)
                SecondaryTextFont.Dispose();
            if (BackgroundImage != null)
                BackgroundImage.Dispose();
            if (SelectedTextFont != null)
                SelectedTextFont.Dispose();
            if (SelectedSecondaryTextFont != null)
                SelectedSecondaryTextFont.Dispose();
            if (SelectedBackgroundImage != null)
                SelectedBackgroundImage.Dispose();
        }

        #endregion
    }

    public interface IXFItem : IDisposable
    {
        XFPanelBase Parent { get; set; }
        XFItemState State { get; set; }
        Bitmap Buffer { get; set; }
        XFItemStyle Style { get; set; }
        XFItemType ItemType { get; set; }

        int GetHeight();
        void ResetHeight();
        void ItemPaint(Graphics g, int x, int y);
        XFItemClickResult GetClickResult();
    }

    public interface IXFItemReceiveClick : IXFItem
    {
        void GetClick(int x, int y);
    }


    public class XFItemBack : IXFItem
    {
        private int height = 40;

        public XFItemBack()
        {
            Style = new XFItemStyle()
            {
                PaddingTop = 2
            };
            Style.BackgroundColor = Color.RoyalBlue;
            Style.TextFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
            Style.TextColor = Color.White;
            Style.DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash;

            ItemType = XFItemType.Clickable;
        }

        #region IXFItem Members

        public XFItemType ItemType { get; set; }
        public XFPanelBase Parent { get; set; }
        public XFItemState State { get; set; }
        public Bitmap Buffer { get; set; }
        public XFItemStyle Style { get; set; }

        public int GetHeight()
        {
            return height + Style.PaddingTop + Style.PaddingBottom;
        }

        public void ResetHeight() { }

        public void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            int width = Screen.PrimaryScreen.WorkingArea.Width;
            string loading = "Back";

            XFControlUtils.DrawBackground(Style, g, x, y, width, GetHeight());
            XFControlUtils.DrawBoarders(Style, g, x, y, width, GetHeight());

            if (Parent != null)
            {
                if (Parent.AlphaBarWidth > 0)
                {
                    width -= Parent.AlphaBarWidth;

                    if (Parent.ScrollbarVisible)
                        width -= 5;
                }
            }

            int xmid = width / 2;

            using (SolidBrush b = new SolidBrush(Style.TextColor))
            {
                xmid -= (int)(g.MeasureString(loading, Style.TextFont).Width / 2);
                g.DrawString(loading, Style.TextFont, b, xmid, y + Style.PaddingTop);
            }
        }

        public XFItemClickResult GetClickResult()
        {
            if (Parent == null)
                return XFItemClickResult.Nothing;

            return XFItemClickResult.SlideRight;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (Buffer != null)
                Buffer.Dispose();
            if (Style != null)
                Style.Dispose();

            Style = null;
            Buffer = null;
        }

        #endregion
    }

}
